<?php
/**
 * Autor:   Miguel A. Zilli
 * Email:   miguelzilli@gmail.com
 */

class InstalacionesController extends AppController{
    var $name = 'Instalaciones';
    var $uses = array('Instalacion','Revision','Instalacionestipo','Atributo',
        'Atributosxinstalacion','Predio');
    var $helpers=array('Ajax','Javascript','DatePicker');

    function agregar($id_revision){
        if (!empty($this->data)){
            $this->data['Instalacion']['revision_id']=$id_revision;
            if ($this->Instalacion->save($this->data)){
                $id_inst=$this->Instalacion->getID();
                if (!empty($this->data['Atributosxinstalacion'])){
                    foreach($this->data['Atributosxinstalacion'] as $key=>$data){
                        $this->data['Atributosxinstalacion'][$key]['instalacion_id']=$id_inst;
                        $this->Atributosxinstalacion->save($this->data['Atributosxinstalacion'][$key]);
                        $this->Atributosxinstalacion->create();
                    }
                }
                $this->Session->setFlash('Operacion realizada correctamente.');
                $this->redirect('/revisiones/ver/'.$id_revision);
            }else{
                $this->Session->setFlash('Error: No se pudo realizar la operacion.');
            }
        }
        $revision=$this->Revision->find('first',array('conditions'=>'Revision.id='.$id_revision));
        $this->set('revision',$revision);
        $this->set('predios',$this->Predio->find('list',array(
                    'fields'=>'Predio.dom_pred',
                    'conditions'=>'Predio.productor_id='.$revision['Revision']['productor_id'],
                    'order'=>'Predio.dom_pred'
                )));
        $this->set('instalacionestipos',$this->Instalacionestipo->find('list',array(
                    'fields'=>'desc_tipo_inst',
                    'order'=>'desc_tipo_inst ASC'
                )));
        $primeraInst=$this->Instalacionestipo->find('first',array('order'=>'desc_tipo_inst ASC'));
        $this->set('atributos',$this->Atributo->find('all',array(
                    'conditions'=>'Atributo.instalacionestipo_id='.$primeraInst['Instalacionestipo']['id'],
                    'order'=>'nombre_atr ASC'
                )));
        $this->set('situaciones',array('PROPIA'=>'PROPIA','CONTRATADA'=>'CONTRATADA','OTRA'=>'OTRA'));
        $this->set('siNo',array('SI'=>'SI','NO'=>'NO','N/C'=>'N/C'));
    }

    function editar($id=null){
        if (!empty($this->data)){
            if ($this->Instalacion->save($this->data)){
                if(!empty($this->data['Atributosxinstalacion'])){
                    if (!empty($this->data['Atributosxinstalacion'])){
                        foreach($this->data['Atributosxinstalacion'] as $key=>$data){
                            $this->Atributosxinstalacion->save($this->data['Atributosxinstalacion'][$key]);
                            $this->Atributosxinstalacion->create();
                        }
                    }
                }
                $this->Session->setFlash('Operacion realizada correctamente.');
                $this->redirect('/revisiones/ver/'.$this->data['Instalacion']['revision_id']);
            }else{
                $this->Session->setFlash('Error: No se pudo realizar la operacion.');
            }
        }
        if (empty($this->data)){
            $this->data=$this->Instalacion->read(null,$id);
        }
        $this->data+=$this->Atributosxinstalacion->find('all',array(
                    'conditions'=>'Atributosxinstalacion.instalacion_id='.$id,
                    'order'=>'Atributo.nombre_atr ASC'
            ));
        $this->set('predios',$this->Predio->find('list',array(
                    'fields'=>'Predio.dom_pred',
                    'conditions'=>'Predio.productor_id='.$this->data['Revision']['productor_id'],
                    'order'=>'Predio.dom_pred'
                )));
        $this->set('situaciones',array('PROPIA'=>'PROPIA','CONTRATADA'=>'CONTRATADA','OTRA'=>'OTRA'));
        $this->set('siNo',array('SI'=>'SI','NO'=>'NO','N/C'=>'N/C'));
    }

    function eliminar($id=null){
        $id_revision=$this->Instalacion->field('Instalacion.revision_id',array('Instalacion.id'=>$id));
        if($this->Instalacion->del($id)){
            $this->Session->setFlash('Operacion realizada correctamente.');
            $this->redirect('/revisiones/ver/'.$id_revision);
        }else{
            $this->Session->setFlash('Error: No se pudo realizar la operacion.');
            $this->redirect('/revisiones/ver/'.$id_revision);
        }
    }

    function update_atributos(){
        Configure::write("debug",0);
        if (!empty($this->data['Instalacion']['instalacionestipo_id'])){
            $id_tipoInst= $this->data['Instalacion']['instalacionestipo_id'];
            $atributos=$this->Atributo->find('all',array(
                    'conditions'=>'Atributo.instalacionestipo_id='.$id_tipoInst,
                    'order'=>'nombre_atr ASC'
                ));
        }else{
            $atributos=false;
        }
        $this->set('atributos', $atributos);
        $this->render('/elements/update_atributos', 'ajax');
    }

}
?>